package cn.itcast.bos.service.base.impl;

import java.util.List;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.ICourierRepository;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.service.base.ICourierService;

@Service
@Transactional
public class CourierServiceImpl implements ICourierService {

    @Autowired
    private ICourierRepository courierRepository;

    // 添加取派员
    @Override
    // Apache Shiro 实现方法级别细粒度权限控制
    // @RequiresPermissions("courier_add")
    public void saveCourier(Courier courier) {
        courierRepository.save(courier);

    }

    // 分页查询取派员
    @Override
    public Page<Courier> pageQueryCourier(Specification spec, Pageable pageable) {
        // TODO Auto-generated method stub
        return courierRepository.findAll(spec, pageable);
    }

    // 作废取派员
    @Override
    public void delCourier(String[] idArray) {
        // TODO Auto-generated method stub
        for (String id : idArray) {
            courierRepository.updateDelTag(Integer.parseInt(id));
        }
    }

    // 查询未排版的取派员
    @Override
    public List<Courier> findNoAssociation() {
        // 添加条件
        Specification<Courier> specification = new Specification<Courier>() {

            @Override
            public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate p = cb.isEmpty(root.get("fixedAreas").as(Set.class));
                return p;
            }
        };
        return courierRepository.findAll(specification);
    }

}
